package app.server.s02;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.agile.jfinal.utils.DataUtils;

import pub.utils.RunTimeException;
import app.model.AppBasedoc;
import app.model.AppClient;


/**
 * 消息服务器通讯管理
 * @author qiutian
 *
 */
public class AppServerManage {

	/**
	 * 传入数据生成一条client信息，返回 服务端信息
	 * @param appCode appCode  App程序的编码
	 * @param mapClient  
	 * 		map.put("vcode","客户端设备编码");
	 *		map.put("vname","客户端设备名称");
	 *		map.put("is_online","是否上线1-是 0-否");
	 *		map.put("start_time","上线时间");
	 *		map.put("end_time","下线时间");
	 *		map.put("version","版本号");
	 *		map.put("ip_address","IP地址");
	 *		map.put("gps_address","GPS定位信息");
	 *
	 * @return map.put("is_update","是否更新1-是 0-否);
	 *	       map.put("pic_welcome","欢迎界面链接");
	 *	       map.put("app_url","主页面链接");
	 *	       --map.put("app_menu","自定义菜单");
	 *
	 * @author qiutian
	 * @throws RunTimeException 当不存在APP对应的appCode时，抛出此异常。
	 * @date 2014-03-25
	 */
	@SuppressWarnings("unused")
	public Map<String, Object> connectServer(String appCode, Map<String, Object> mapClient) throws RunTimeException {
		//查询APP档案，获取APP的最新信息
		AppBasedoc appBasedoc = new AppBasedoc().set(AppBasedoc.VCODE, appCode).queryFirst();
		if(appBasedoc != null){
			
			//获取客户端信息并更新
			Map<String,Object> realmapClient= getRealAtt(AppClient.class, mapClient);
			
			AppClient appClient = new AppClient();
			appClient.set(appClient.VCODE, mapClient.get(appClient.VCODE));
			appClient = appClient.queryFirst();
			if(null == appClient){								//如果不存在,插入一条新的数据
				appClient = new AppClient();
				appClient.setAttrs(realmapClient); 					//设置全部数据
				appClient.set(appClient.PK_BASEDOC, appBasedoc.getPrimaryValue());
				appClient.save();
			}else{ 												//存在则更新数据
				int is_online = DataUtils.toInteger(mapClient.get(appClient.IS_ONLINE));
				if(0==is_online){ 								//下线计算在线时间，暂时未实现
					Date online_time = appClient.getDate(appClient.ONLINE_TIME);
				}else{											//上线
				}
				appClient.setAttrs(realmapClient).save();
			}
			
			/*生成APP数据的返回信息*/ 
			boolean is_update = appBasedoc.getBooleanByInt(appBasedoc.IS_UPDATE);
			if(is_update){ //返回发送客户端的数据
				Map<String,Object> mapAppBasedoc = new HashMap<String, Object>();
				mapAppBasedoc.put("pic_welcome",appBasedoc.get(appBasedoc.PIC_WELCOME));
				mapAppBasedoc.put("app_url",appBasedoc.get(appBasedoc.APP_URL));
				mapAppBasedoc.put("is_update",appBasedoc.getInt(appBasedoc.IS_UPDATE));
//				mapAppBasedoc.put("app_menu","自定义菜单");
				return mapAppBasedoc;
			}else{
				return null;
			}
		}else{
//			throw new JFBaseException("app001");
			return null;
		}
	}
	
	/**
	 * 删除不属于该类属性的元素
	 * @param theclass
	 * @param map
	 * @return
	 */
	public Map<String,Object> getRealAtt(Class theclass,Map<String,Object> map){
		/*获取该类中的所有属性*/
		Field[] fields = theclass.getFields();
		Set<String> fieldsSet = new HashSet<String>();
		for(Field field:fields){
			fieldsSet.add(field.getName().toLowerCase());
		}
		/*遍历该map，删除掉不属于该类属性的元素*/
		Map<String,Object> mymap = new HashMap<String,Object>();
		Iterator iter = map.entrySet().iterator(); 
		while (iter.hasNext()) { 
		    Map.Entry entry = (Map.Entry) iter.next(); 
		    String key = (String)entry.getKey(); 
		    Object val = (Object)entry.getValue(); 
		    if(fieldsSet.contains(key)){
		    	mymap.put(key, val);
		    }
		}
		return mymap;
	}
}
